<?php
    class LocationControl extends Control {
        protected static $_callable = array(
            "delete",
            "getList",
            "load",
            "save"
        );
        
        /**
         * Funktio poistaa harjoituspaikan tiedot tietokannasta.
         *
         * @return bool
         */
        public static function delete($data) {
            $result = false;
            
            DB::begin();
            
            try {
                $location = new LocationModel(array(
                    "hashid" => arrGet($data,"hashid")
                ));
                
                # Tarkasta onko harjoituspaikka liitetty harjoituksiin
                $query = DB::preparedQuery("
                    SELECT id
                    FROM workout
                    WHERE locationid=:locationid
                    LIMIT 1
                ",array(
                    "locationid" => $location->id
                ));
                if (DB::numRows($query) == 1) {
                    DB::freeResult($query);
                    throw new DBException(
                        "Location is attached to one or more workouts",
                        40,
                        __CLASS__,
                        "Location is attached to one or more workouts"
                    );
                }
                
                $location->delete();
                
                $result = true;
            } catch (Exception $e) {
                DB::rollback();
                throw $e;
            }
            
            DB::commit();
            
            return $result;
        }
        
        /**
         * Funktio listaa käytettävissä olevat harjoituspaikat.
         *
         * @return  array
         */
        public static function getList() {
            $result = array();
            
            $list = DB::select("
                SELECT hashid,location
                FROM location
                WHERE userid=:userid
                ORDER BY location,id
                ",array(
                    "userid" => LoginControl::get("userid")
                )
            );
            foreach ($list as $row) {
                $result[$row["hashid"]] = $row["location"];
            }
            
            return $result;
        }
        
        /**
         * Funktio hakee harjoituspaikan tiedot tietokannasta.
         *
         * @return  array
         */
        public static function load($data) {
            $result = array();
            
            $location = new LocationModel(array(
                "hashid" => arrGet($data,"hashid")
            ));
            
            $result["hashid"]   = $location->hashid;
            $result["location"] = $location->location;
            $result["updated"]  = $location->updated;
            $result["rowstamp"] = $location->rowstamp;
            
            return $result;
        }
        
        /**
         * Tallentaa harjoituspaikan tiedot ja palauttaa paikan hashid:n.
         *
         * @param   array   $data   Tiedot
         * @return  string
         */
        public static function save($data) {
            DB::begin();
            
            try {
                $arr = null;
                $hashid = arrGet($data,"hashid");
                if (isset($hashid)) {
                    $arr = array("hashid" => $hashid);
                }
                
                $location = new LocationModel($arr);
                
                # Aseta userid jos luodaan uutta
                if (!isset($hashid)) {
                    $location->userid = LoginControl::get("userid");
                } else {                    
                    # Aseta päivitysaika
                    $location->updated = date("Y-m-d H:i:s");
                }
                
                $location->location = $data;
                  
                $location->write();
                $hashid = $location->hashid;
            } catch (Exception $e) {
                DB::rollback();
                throw $e;
            }
            
            DB::commit();
            
            return $hashid;
        }
    }